home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 133 / c / fdump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-17  |  4.4 KB  |  91 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     fdump.c - Display contents of a file                         */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       January 8, 1987                                              */
  8. /*                                                                          */
  9. /* Function:   Display the contents of a file in hex ans ASCII              */
  10. /*                                                                          */
  11. /****************************************************************************/
  12.  
  13. #include <stdio.h>
  14.  
  15.  
  16. main(argc,argv)
  17. int     argc;
  18. char    *argv[];
  19.  
  20. {
  21.  
  22.   int i;
  23.   FILE  *fp;
  24.  
  25.   if (argc > 1)                         /* if multiple arguments,           */
  26.     {
  27.       for (i = 1; i < argc; i++)        /* then do each one                 */
  28.         {
  29.           printf("******************************\n"); /* set up header      */
  30.           printf("%s\n", argv[i]);      /* print file name                  */
  31.           printf("******************************\n");
  32.           if( (fp = fopenb(argv[i],"r") ) == NULL) /* if error opening file, */
  33.             {
  34.               fprintf(stderr,"%s: Unable to open %s \n",argv[0],argv[i]);
  35.               continue;
  36.             }
  37.           else                          /* if file opened OK,               */
  38.             {
  39.               dump(fp);                 /* print it                         */
  40.               printf("\f\n");           /* tack on a form feed              */
  41.             }                           /* end file opened OK               */
  42.         }                               /* end for one argument             */
  43.     }                                   /* end for all arguments            */
  44.     else                                /* if no arguments,                 */
  45.       dump(stdin);                      /* dump standard in                 */
  46. }                                       /* end main                         */
  47.  
  48. /****************************************************************************/
  49. /* dump - hex dump                                                          */
  50. /****************************************************************************/
  51. dump(fp)
  52. FILE *fp;                               /* file to dump                     */
  53.  
  54. {
  55.   static char  data[] = "                "; /* build ASCII image here       */
  56.   int    i = 0;
  57.   long   count = 0L;
  58.   int    ch;
  59.  
  60.   while(( ch=fgetc(fp)) != EOF)         /* for each character,              */
  61.     {
  62.       if(i == 0)                        /* if at the beginning of a line,   */
  63.         printf("%06X ",count);          /* print the byte address           */
  64.  
  65.       printf("%02x ",(int) (ch & 0xff) ); /* then the byte                  */
  66.  
  67.       if(i == 7)
  68.         printf("  ");                   /* stick extra break in center      */
  69.  
  70.       if( ( ch > 0x1f) && (ch < 0x7f) ) /* if character is printable,       */
  71.         data[i++] = ch;                 /* use it.                          */
  72.       else                              /* if not printable,                */
  73.         data[i++] = '.';                /* use a period                     */
  74.  
  75.       if(i == 16)                       /* if a full line is done,          */
  76.         {
  77.           printf("  %s \n",data);       /* print the ASCII version,         */
  78.           i = 0;                        /* reset the line count             */
  79.           count += 16;                  /* and increment the byte address   */
  80.         }                               /* end of one line                  */
  81.     }                                   /* end of file                      */
  82.   while (i < 16)                        /* if not multiple of 16 bytes      */
  83.     {
  84.       printf("   ");                    /* space over                       */
  85.       data[i++] = ' ';                  /* align ASCII portion of line      */
  86.     }
  87.   printf("  %s \n",data);               /* print ASCII for last line        */
  88.  
  89.   fclose(fp);                           /* close current file               */
  90. }
  91.